$(function(){
	//重置
	$('#btn-reset').click(function(){
		$('#search-hallName').val("");
	
		/*var p ={p:null}
		selectAllCategoryInfo(p);*/
	})
	//查询所有影厅信息
	var p ={p:null}
	selectAllUserInfo(p);
	function selectAllUserInfo(p){
		  $('#user-order').empty();
		  $('#user-order').append('<tr><td></td><td  hidden="true">主键号</td> <td>序号</td> <td>影厅名称</td><td>影厅别名</td><td>座位信息</td></tr>');
	$.ajax({
		  type:"POST",
		  url:"/movie.front/hallConntroller/findAllHall",
		  data:JSON.stringify(p),
		  dataType:"json",
		  contentType:"application/json",
		  success:function(data){
           array = data.result;
          
           for(var i=0;i<array.length;i++){
        	   var html = '' ;
        	  
        		   html = html+'<tr><td><input type="checkbox" id="'+array[i].id+'"></td><td  hidden="true">'+array[i].id+'</td><td>'+(i+1)+'</td><td>'+array[i].hallName+'</td><td>'+array[i].hallAlias+'</td><td>'+(array[i].seatsx+1)+'排'+(array[i].seatsy+1)+'座</td></tr>';
           	    $('#user-order').append(html); 
        	   
        	   
           }
		   
		   
          }
	
	   });
		
	}
	//查询信息
	$('#btn-search').click(function(){
    var hallName =	$('#search-hallName')[0].value;
	
	 var p = {hallName:hallName};
	 selectAllUserInfo(p);
	})
	
	$('#btn-insert').click(function(){
		$('#hallName').val("");
		$('#hallAlias').val("");
		$('#seatNumberX')[0].value="";
		$('#seatNumberY')[0].value="";
		$('#myModal').modal('show');
		/*alert('aaaaaaa');*/
	});
	//保存用户信息
	$('#btn-save-hall').click(function(){
	var hallName =	$('#hallName')[0].value;
	var hallAlias=$('#hallAlias')[0].value;
	var seatNumberY=$('#seatNumberY')[0].value;
	var seatNumberX=$('#seatNumberX')[0].value;
	var id = $('#id')[0].value;
	if(id==null || id==""){
		var p={id:"",hallName:hallName,hallAlias:hallAlias,seatNumberY:seatNumberY,seatNumberX:seatNumberX};
	$.ajax({
		  type:"POST",
		  url:"/movie.front/hallConntroller/addHall",
		  data:JSON.stringify(p),
		  dataType:"json",
		  contentType:"application/json",
		  success:function(data){
            if(data.successCode!=null){
            	//关闭对话框
            	$('#myModal').modal('hide');
            	//刷新表格
            	var d={d:null}
            	selectAllUserInfo(d);
            }
            if(data.errorcode!=null){
            	alert(data.errorcode);
            }
		   
		  }
	
	   });
	}else{
		var p={id:id,hallName:hallName,hallAlias:hallAlias};
		$.ajax({
			  type:"POST",
			  url:"/movie.front/hallConntroller/updateHall",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
				  if(data.successCode!=null){
					  $('#myModal').modal('hide');
						//刷新表格
			            	var d={d:null}
			            	selectAllUserInfo(d);					  
				  }
				  if(data.errorcode!=null){
		            	alert(data.errorcode);
		            }

			   
			  }
		
		   });
	 }
		
	});
	
	//修改用户信息
	$('#btn-update').click(function(){
		var flag=0;
		var id =0;
		var array = $(("input[type='checkbox']"));
		for(var i=0;i<array.length;i++){
		  console.info(array[i].id);
		  if($('#'+array[i].id).is(':checked')){
			var id = array[i].id;
		      flag++;
		      if(flag>1){
		    	  alert("对不起，只能选择单行");
		    	return false;
		      }else{
		    	  id=array[i].id;
		      }
		
		  }
	   
		}
		  if(flag==1){
			  $('#myModal').modal('show');
			  var id = {id:id};
			  getByIdUserInfo(id);
		     }
	});
	
	//删除用户信息
	$('#btn-remove').click(function(){
		var flag=0;
		var id=0;
		var array = $(("input[type='checkbox']"));
		for(var i=0;i<array.length;i++){
		  console.info(array[i].id);
		  if($('#'+array[i].id).is(':checked')){
		      flag++;
		      if(flag>1){
		    	  alert("对不起，只能选择单行");
		    	 return false;
		      }else{
		    	  var id = array[i].id;
		      }
		
		  }
	   
		}
		  if(flag==1){
		    	 var id = {id:id};
		    	 deleteUserInfo(id);
		      }
		
	});
	function deleteUserInfo(p){
		$.ajax({
			  type:"POST",
			  url:"/movie.front/hallConntroller/deleteHall",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
				  if(data.successCode!=null){
					  alert("删除成功");
					  $('#myModal').modal('hide');
						//刷新表格
			            	var d={d:null}
			            	selectAllUserInfo(d);					  
				  }
				  if(data.errorcode!=null){
		            	alert(data.errorcode);
		            }
				 
				 
				  
			     }
		   });
	}
	function getByIdUserInfo(p){
		//回显信息
	
		$.ajax({
			  type:"POST",
			  url:"/movie.front/hallConntroller/getHallById",
			  data:JSON.stringify(p),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){
	           $('#hallName')[0].value=data.result.hallName;
	           $('#hallAlias')[0].value=data.result.hallAlias;
	           $('#id')[0].value=data.result.id;
	           $('#seatNumberX')[0].value=data.result.seatNumberX+1;
	           $('#seatNumberY')[0].value=data.result.seatNumberY+1;
			  }
		
		   });
	}
	

 });
